******************
** Load dataset **
******************
** Set folder with replication files as working directory
cd "C:\FOLDER\"

import delimited "data.csv", clear
xtset id yr


** Run next two commands to create required sub-folders if running code for the first time
// mkdir Results
// mkdir Plots



****************
** Table 1/B1 **
****************
tabstat pspn_congruence psns_congruence v2lgdomchm dm_ratio seat_ratio abs_time_diff partial_elec v2xnp_pres yrs_since_pres if (lc_elec_year==1 | uc_elec_year==1), c(s) s(n mean sd min max) f(%9.4fc) 




**************
** Table B2 **
**************
vl create controlvar = (v2lgdomchm dm_ratio seat_ratio abs_time_diff partial_elec v2xnp_pres yrs_since_pres)

// Full sample
sureg (pspn_congruence $controlvar) (psns_congruence $controlvar) if (lc_elec_year==1 | uc_elec_year==1), vce(cluster id) corr
gen sample1=_n if e(sample)
sort sample1
replace sample1=_n if sample1!=.
sort id yr
esttab using "Results\cause.csv", replace se nogaps b(4) se(4) level(90) star(* 0.10 ** 0.05 *** 0.01) unstack 
esttab using "Results\cause_plot_90.csv", replace nostar ci level(90) noparentheses wide
esttab using "Results\cause_plot_95.csv", replace nostar ci level(95) noparentheses wide


// Non-US sample
sureg (pspn_congruence $controlvar) (psns_congruence $controlvar) if (lc_elec_year==1 | uc_elec_year==1) & ctr_n!="United States", vce(cluster id)
esttab using "Results\cause_nonus.csv", replace se nogaps b(4) se(4) level(90) star(* 0.10 ** 0.05 *** 0.01) unstack
esttab using "Results\cause_plot_nonus_90.csv", replace nostar ci level(90) noparentheses wide
esttab using "Results\cause_plot_nonus_95.csv", replace nostar ci level(95) noparentheses wide


// Democracy sample
sureg (pspn_congruence $controlvar) (psns_congruence $controlvar) if (lc_elec_year==1 | uc_elec_year==1) & v2x_regime>=2, vce(cluster id)
esttab using "Results\cause_dem.csv", replace se nogaps b(4) se(4) level(90) star(* 0.10 ** 0.05 *** 0.01) unstack
esttab using "Results\cause_plot_dem_90.csv", replace nostar ci level(90) noparentheses wide
esttab using "Results\cause_plot_dem_95.csv", replace nostar ci level(95) noparentheses wide





**************
** Table C1 **
**************
tabstat wb_gov_spending v2lgdomchm_ord_dum pspn_congruence psns_congruence wg_n_party mean_enp v2xnp_pres ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if v2x_regime>=2 & yr>=1970, c(s) s(n mean sd min max) f(%9.4fc) 




**************
** Table C2 **
**************
// Model 1
xtpcse wb_gov_spending l.wb_gov_spending i.l.v2lgdomchm_ord_dum##c.l.pspn_congruence i.l.v2lgdomchm_ord_dum##c.l.psns_congruence l.wg_n_party l.mean_enp l.v2xnp_pres l.ln_wb_unemp l.ln_wb_trade l.ln_wb_age_dep l.ln_wb_gdppc l.ln_wb_inflation ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if v2x_regime>=2, p
esttab using "Results\consq.csv", replace se nogaps b(4) se(4) level(90) star(* 0.10 ** 0.05 *** 0.01) drop(0bL.v2lgdomchm_ord_dum 0bL.v2lgdomchm_ord_dum#coL.pspn_congruence 0bL.v2lgdomchm_ord_dum#coL.psns_congruence)

gen sample2=_n if e(sample)
sort sample2
replace sample2=_n if sample2!=.
sort id yr

margins l.v2lgdomchm_ord_dum, dydx(l.pspn_congruence l.psns_congruence) l(90)

mat B=e(b)
esttab mat(B) using "Results\consq_coef.csv", replace mlab(none)
mat V=e(V)
esttab mat(V) using "Results\consq_vcov.csv", replace mlab(none)

test _b[L.pspn_congruence]=_b[1L.v2lgdomchm_ord_dum#cL.pspn_congruence]=0
test _b[L.psns_congruence]=_b[1L.v2lgdomchm_ord_dum#cL.psns_congruence]=0


qui xtpcse wb_gov_spending l.wb_gov_spending i.l.v2lgdomchm_ord_dum##c.l.pspn_congruence i.l.v2lgdomchm_ord_dum##c.l.psns_congruence l.wg_n_party l.mean_enp l.v2xnp_pres l.ln_wb_unemp l.ln_wb_trade l.ln_wb_age_dep l.ln_wb_gdppc l.ln_wb_inflation ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if v2x_regime>=2, p
qui margins l.v2lgdomchm_ord_dum, dydx(l.pspn_congruence l.psns_congruence) l(90) post
esttab using "Results\consq_90.csv", replace nostar ci noparentheses wide level(90)

qui xtpcse wb_gov_spending l.wb_gov_spending i.l.v2lgdomchm_ord_dum##c.l.pspn_congruence i.l.v2lgdomchm_ord_dum##c.l.psns_congruence l.wg_n_party l.mean_enp l.v2xnp_pres l.ln_wb_unemp l.ln_wb_trade l.ln_wb_age_dep l.ln_wb_gdppc l.ln_wb_inflation ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if v2x_regime>=2, p
qui margins l.v2lgdomchm_ord_dum, dydx(l.pspn_congruence l.psns_congruence) l(95) post
esttab using "Results\consq_95.csv", replace nostar ci noparentheses wide level(95)


// Model 2
xtpcse wb_gov_spending l.wb_gov_spending i.l.v2lgdomchm_ord_dum##c.l.pspn_congruence i.l.v2lgdomchm_ord_dum##c.l.psns_congruence l.wg_n_party l.mean_enp l.v2xnp_pres l.ln_wb_unemp l.ln_wb_trade l.ln_wb_age_dep l.ln_wb_gdppc l.ln_wb_inflation l.cbie ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if v2x_regime>=2, p
esttab using "Results\consq_cbi.csv", replace se nogaps b(4) se(4) level(90) star(* 0.10 ** 0.05 *** 0.01) drop(0bL.v2lgdomchm_ord_dum 0bL.v2lgdomchm_ord_dum#coL.pspn_congruence 0bL.v2lgdomchm_ord_dum#coL.psns_congruence)

test _b[L.pspn_congruence]=_b[1L.v2lgdomchm_ord_dum#cL.pspn_congruence]=0
test _b[L.psns_congruence]=_b[1L.v2lgdomchm_ord_dum#cL.psns_congruence]=0

qui xtpcse wb_gov_spending l.wb_gov_spending i.l.v2lgdomchm_ord_dum##c.l.pspn_congruence i.l.v2lgdomchm_ord_dum##c.l.psns_congruence l.wg_n_party l.mean_enp l.v2xnp_pres l.ln_wb_unemp l.ln_wb_trade l.ln_wb_age_dep l.ln_wb_gdppc l.ln_wb_inflation l.cbie ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if v2x_regime>=2, p
qui margins l.v2lgdomchm_ord_dum, dydx(l.pspn_congruence l.psns_congruence) l(90) post
esttab using "Results\consq_cbi_90.csv", replace nostar ci noparentheses wide level(90)

qui xtpcse wb_gov_spending l.wb_gov_spending i.l.v2lgdomchm_ord_dum##c.l.pspn_congruence i.l.v2lgdomchm_ord_dum##c.l.psns_congruence l.wg_n_party l.mean_enp l.v2xnp_pres l.ln_wb_unemp l.ln_wb_trade l.ln_wb_age_dep l.ln_wb_gdppc l.ln_wb_inflation l.cbie ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if v2x_regime>=2, p
qui margins l.v2lgdomchm_ord_dum, dydx(l.pspn_congruence l.psns_congruence) l(95) post
esttab using "Results\consq_cbi_95.csv", replace nostar ci noparentheses wide level(95)



// Model 3
xtpcse wb_gov_spending l.wb_gov_spending i.l.v2lgdomchm_ord_dum##c.l.pspn_congruence i.l.v2lgdomchm_ord_dum##c.l.psns_congruence l.wg_n_party l.mean_enp l.v2xnp_pres l.ln_wb_unemp l.ln_wb_trade l.ln_wb_age_dep l.ln_wb_gdppc l.ln_wb_inflation l.leftgov1 ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if v2x_regime>=2, p
esttab using "Results\consq_leftgov.csv", replace se nogaps b(4) se(4) level(90) star(* 0.10 ** 0.05 *** 0.01) drop(0bL.v2lgdomchm_ord_dum 0bL.v2lgdomchm_ord_dum#coL.pspn_congruence 0bL.v2lgdomchm_ord_dum#coL.psns_congruence)

test _b[L.pspn_congruence]=_b[1L.v2lgdomchm_ord_dum#cL.pspn_congruence]=0
test _b[L.psns_congruence]=_b[1L.v2lgdomchm_ord_dum#cL.psns_congruence]=0

qui xtpcse wb_gov_spending l.wb_gov_spending i.l.v2lgdomchm_ord_dum##c.l.pspn_congruence i.l.v2lgdomchm_ord_dum##c.l.psns_congruence l.wg_n_party l.mean_enp l.v2xnp_pres l.ln_wb_unemp l.ln_wb_trade l.ln_wb_age_dep l.ln_wb_gdppc l.ln_wb_inflation l.leftgov1 ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if v2x_regime>=2, p
qui margins l.v2lgdomchm_ord_dum, dydx(l.pspn_congruence l.psns_congruence) l(90) post
esttab using "Results\consq_leftgov_90.csv", replace nostar ci noparentheses wide level(90)

qui xtpcse wb_gov_spending l.wb_gov_spending i.l.v2lgdomchm_ord_dum##c.l.pspn_congruence i.l.v2lgdomchm_ord_dum##c.l.psns_congruence l.wg_n_party l.mean_enp l.v2xnp_pres l.ln_wb_unemp l.ln_wb_trade l.ln_wb_age_dep l.ln_wb_gdppc l.ln_wb_inflation l.leftgov1 ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if v2x_regime>=2, p
qui margins l.v2lgdomchm_ord_dum, dydx(l.pspn_congruence l.psns_congruence) l(95) post
esttab using "Results\consq_leftgov_95.csv", replace nostar ci noparentheses wide level(95)




*********************
** Figure D1/D2/D3 **
*********************
// Figure D1
foreach i of numlist 1/17{
	eststo: qui sureg (pspn_congruence $controlvar) (psns_congruence $controlvar) if (lc_elec_year==1 | uc_elec_year==1) & id!=`i', vce(cluster id) corr
}
esttab using "Results\cause_jk_plot_90.csv", replace nostar ci level(90) noparentheses wide
esttab using "Results\cause_jk_plot_95.csv", replace nostar ci level(95) noparentheses wide
eststo clear



// Figure D2
foreach i of numlist 1/17{
	qui xtpcse wb_gov_spending l.wb_gov_spending i.l.v2lgdomchm_ord_dum##c.l.pspn_congruence i.l.v2lgdomchm_ord_dum##c.l.psns_congruence l.wg_n_party l.mean_enp l.v2xnp_pres l.ln_wb_unemp l.ln_wb_trade l.ln_wb_age_dep l.ln_wb_gdppc l.ln_wb_inflation ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if id!=`i' & v2x_regime>=2, p
	eststo: qui margins l.v2lgdomchm_ord_dum, dydx(l.pspn_congruence l.psns_congruence) l(90) post
}

esttab using "Results\consq_jk_90.csv", replace nostar ci noparentheses wide level(90)
eststo clear


foreach i of numlist 1/17{
	qui xtpcse wb_gov_spending l.wb_gov_spending i.l.v2lgdomchm_ord_dum##c.l.pspn_congruence i.l.v2lgdomchm_ord_dum##c.l.psns_congruence l.wg_n_party l.mean_enp l.v2xnp_pres l.ln_wb_unemp l.ln_wb_trade l.ln_wb_age_dep l.ln_wb_gdppc l.ln_wb_inflation ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if id!=`i' & v2x_regime>=2, p
	eststo: qui margins l.v2lgdomchm_ord_dum, dydx(l.pspn_congruence l.psns_congruence) l(95) post
}

esttab using "Results\consq_jk_95.csv", replace nostar ci noparentheses wide level(95)
eststo clear




**************************************************
** Additional: Individual observation jackknife **
**************************************************
// Re-estimate Table B2 iteratively dropping individual observations
foreach i of numlist 1/153{
	eststo: qui sureg (pspn_congruence $controlvar) (psns_congruence $controlvar) if (lc_elec_year==1 | uc_elec_year==1) & sample1!=`i', vce(cluster id) corr
}
esttab using "Results\cause_ijk.csv", replace se nogaps b(4) se(4) level(90) star(* 0.10 ** 0.05 *** 0.01) unstack 
eststo clear


// Re-estimate Model 1 in Table C2 and calculate average marginal effects of partisan/nationalization congruence iteratively dropping observations
foreach i of numlist 1/200{
	qui xtpcse wb_gov_spending l.wb_gov_spending i.l.v2lgdomchm_ord_dum##c.l.pspn_congruence i.l.v2lgdomchm_ord_dum##c.l.psns_congruence l.wg_n_party l.mean_enp l.v2xnp_pres l.ln_wb_unemp l.ln_wb_trade l.ln_wb_age_dep l.ln_wb_gdppc l.ln_wb_inflation ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if sample2!=`i' & v2x_regime>1, p
	eststo: qui margins l.v2lgdomchm_ord_dum, dydx(l.pspn_congruence l.psns_congruence) l(90) post
}
esttab using "Results\consq_ijk.csv", replace se nogaps b(4) se(4) level(90) star(* 0.10 ** 0.05 *** 0.01)
eststo clear

foreach i of numlist 201/311{
	qui xtpcse wb_gov_spending l.wb_gov_spending i.l.v2lgdomchm_ord_dum##c.l.pspn_congruence i.l.v2lgdomchm_ord_dum##c.l.psns_congruence l.wg_n_party l.mean_enp l.v2xnp_pres l.ln_wb_unemp l.ln_wb_trade l.ln_wb_age_dep l.ln_wb_gdppc l.ln_wb_inflation ln_wb_unemp ln_wb_trade ln_wb_age_dep ln_wb_gdppc ln_wb_inflation if sample2!=`i' & v2x_regime>1, p
	eststo: qui margins l.v2lgdomchm_ord_dum, dydx(l.pspn_congruence l.psns_congruence) l(90) post
}

esttab using "Results\consq_ijk.csv", append se nogaps b(4) se(4) level(90) star(* 0.10 ** 0.05 *** 0.01)
eststo clear